import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TTTHumanPlayer implements TTTPlayer
{
	
	public TTTHumanPlayer()
	{
		// You can implement this constructor, but it may not be necessary.
		//throw new RuntimeException("You must implement this method!");
	}
	
	public TTTMove getMove(TTTBoard board, int player)
	{
		while(true){
			System.out.println("please input the move you want to make Eg. 5,3!");
			try{
				InputStreamReader stream = new InputStreamReader(System.in);
				BufferedReader br = new BufferedReader(stream);
				String input = br.readLine();
			
			
				// parse the input
				String[] arg = input.split(",");
				if (arg.length < 2){
					System.out.println("You must input both x,y coords with delimiter \",\" ");
					continue;
				}	
					
				int row = Integer.parseInt(arg[0]);
				int col = Integer.parseInt(arg[1]);
				
				// valid the input 
				if ( row < 0 || row >= board.getRows() || col < 0 || col >= board.getCols()){
					System.out.println("Please input coords within the board");
					continue;
				}
				if ( board.getBoardData(row, col) != board.EMPTY){
					System.out.println("This position has been taken, please choose another one");
					continue;
				}
				
				return new TTTMove(row,col,player);
			}
			catch (NumberFormatException ne){
				System.out.println("Coords must be numbers!");
			}
			catch (Exception e){
				System.err.println("Error: " + e);
			}
			
			
		}
		
		//throw new RuntimeException("You must implement this method!");
	}
	
}
